﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Shared;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;

namespace NotebookS2
{
    public partial class PRNT_INV : RibbonForm
    {
        public PRNT_INV()
        {
            InitializeComponent();
        }
        public string ConnectionString { get; set; }
        public string DocumentNumber { get; set; }
        public string FileName { get; set; }

        public string GetLayout()
        {
            ObjectTypeManager oManager = new ObjectTypeManager();
            const int _DOCUMENT_TYPE = 5;
            oManager.ConnectionString = this.ConnectionString;

            oManager.Open();

            string _FILE_NAME = oManager.GetPrintLayout1(_DOCUMENT_TYPE);

            oManager.Close();

            return _FILE_NAME;
        }

        //public string GetLayout()
        //{
        //    string sPath = System.Windows.Forms.Application.StartupPath.ToString() + "\\config.conf";
        //    string _OUTPUT_STRING = "";

        //    if (File.Exists(sPath) == true)
        //    {


        //        StreamReader sr = File.OpenText(sPath);
        //        string input;

        //        do
        //        {
        //            input = sr.ReadLine();
        //            if (input != null)
        //            {

        //                if (input.StartsWith("INVLayout") == true)
        //                {
        //                    int iStart = input.IndexOf("=");
        //                    _OUTPUT_STRING = input.Substring(iStart + 1);

        //                    break;
        //                }

        //            }
        //        } while (input != null);


        //    }
        //    return _OUTPUT_STRING;

        //}

        ReportDocument rptDoc = new ReportDocument();
        SqlConnection oConnection = new SqlConnection();
        SqlCommand oCommand = new SqlCommand();
        SqlDataAdapter oAdapter = new SqlDataAdapter();
        DataSet ds = new DataSet();
        public void ViewInvoiceLayout(string sDocNumber)
        {
            rptDoc = new ReportDocument();
            oConnection = new SqlConnection();
            oCommand = new SqlCommand();
            oAdapter = new SqlDataAdapter();
            ds = new DataSet();

            try{
            string sReportFile = GetLayout();

            rptDoc.Load(sReportFile);



            this.DocumentNumber = sDocNumber;

            oConnection.ConnectionString = this.ConnectionString;
            oConnection.Open();

            oCommand.Connection = oConnection;
            oCommand.CommandType = CommandType.StoredProcedure;
            oCommand.CommandText = "SP_LAYOUT_INV";

            oCommand.Parameters.Add(new SqlParameter("@DocNum", sDocNumber));
            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(ds);

            oConnection.Close();


            rptDoc.SetDataSource(ds.Tables[0]);
            CRViewer.ReportSource = rptDoc;
            CRViewer.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }
        private void PRNT_INVOICE_Load(object sender, EventArgs e)
        {
            ViewInvoiceLayout(this.DocumentNumber);
        }

        private void PRNT_INV_FormClosing(object sender, FormClosingEventArgs e)
        {
            rptDoc.Dispose();
            oConnection.Dispose();
            oCommand.Dispose();
            oAdapter.Dispose();
            ds.Dispose();
        }

        private void barExit_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.Close();
        }

        private void barPrint_ItemClick(object sender, ItemClickEventArgs e)
        {
            rptDoc = new ReportDocument();
            oConnection = new SqlConnection();
            oCommand = new SqlCommand();
            oAdapter = new SqlDataAdapter();
            ds = new DataSet();

            try
            {
                string sReportFile = GetLayout();

                rptDoc.Load(sReportFile);



                

                oConnection.ConnectionString = this.ConnectionString;
                oConnection.Open();

                oCommand.Connection = oConnection;
                oCommand.CommandType = CommandType.StoredProcedure;
                oCommand.CommandText = "SP_LAYOUT_INV";

                oCommand.Parameters.Add(new SqlParameter("@DocNum", this.DocumentNumber));
                oAdapter.SelectCommand = oCommand;
                oAdapter.Fill(ds);

                oConnection.Close();


                System.Drawing.Printing.PrintDocument printDoc = new System.Drawing.Printing.PrintDocument();
                printDoc.PrinterSettings.PrinterName = "FX1175";

                int iCount = 0;

                for (iCount = 0; iCount <= printDoc.PrinterSettings.PaperSizes.Count - 1; iCount++)
                {
                    if (printDoc.PrinterSettings.PaperSizes[iCount].PaperName == "Nutratech Invoice")
                    {

                        rptDoc.PrintOptions.PaperSize = (CrystalDecisions.Shared.PaperSize)printDoc.PrinterSettings.PaperSizes[iCount].RawKind;
                        rptDoc.PrintOptions.PaperSource = (CrystalDecisions.Shared.PaperSource)iCount;
                        break;
                    }
                }

                rptDoc.SetDataSource(ds.Tables[0]);

                ReportDocument crpt = rptDoc;

                crpt.PrintOptions.PaperSize = rptDoc.PrintOptions.PaperSize;
                crpt.PrintToPrinter(1, false, 0, 0);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void barRefresh_ItemClick(object sender, ItemClickEventArgs e)
        {
            CRViewer.Refresh();
        }

        private void barExport_ItemClick(object sender, ItemClickEventArgs e)
        {
            CRViewer.ExportReport();
        }
    }
}
